# 多阶段构建 - 构建阶段
FROM node:18-alpine AS builder

WORKDIR /app

# 复制包管理文件
COPY package*.json ./
COPY backend/package*.json ./backend/
COPY frontend/package*.json ./frontend/

# 安装依赖
RUN npm ci --workspace=backend && \
    npm ci --workspace=frontend

# 复制源代码
COPY backend ./backend
COPY frontend ./frontend

# 构建前端
WORKDIR /app/frontend
RUN npm run build

# 构建后端
WORKDIR /app/backend
RUN npm run build

# 生产阶段
FROM node:18-alpine AS production

WORKDIR /app

# 复制构建产物和依赖
COPY --from=builder /app/backend/package*.json ./
COPY --from=builder /app/backend/dist ./dist
COPY --from=builder /app/backend/node_modules ./node_modules
COPY --from=builder /app/frontend/dist ./public

# 暴露端口
EXPOSE 4000

# 启动后端服务（如果需要，可以同时提供静态文件服务）
CMD ["node", "dist/index.js"]




